package com.haolijun.eecms.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.haolijun.eecms.common.R;
import com.haolijun.eecms.dao.TbRepairsDao;
import com.haolijun.eecms.entity.TbFacility;
import com.haolijun.eecms.entity.TbRepairs;
import com.haolijun.eecms.service.FacilityService;
import com.haolijun.eecms.service.RepairsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import static com.haolijun.eecms.common.TransitionUtils.arrayConversion;

/**
 * @ClassName RepairsServiceImpl
 * @Description TODO
 * @Author hljstart
 * @Date 2023/3/12 16:46
 * @Version 1.0
 */
@Service
public class RepairsServiceImpl implements RepairsService {

    @Autowired
    private TbRepairsDao repairsDao;

    @Autowired
    private FacilityService facilityService;

    @Override
    public R searchRepairsByPage(HashMap param) {
        // 1、 获取当前页数
        int page = (int) param.get("page");
        // 2、获取每一页的记录数
        int length = (int) param.get("length");
        // 3、计算出当前的要开始的页数
        int start = (page - 1) * length;
        // 4、放入到集合中
        param.put("start", start);
        // 5、统计总记录数
        long count = repairsDao.searchRepairsCount(param);
        // 6、获取数据
        List<HashMap> repairs = repairsDao.searchRepairsByPage(param);
        // 7、数据格式的转换
        arrayConversion(repairs);
        // 7、封装数据
        R r = R.ok().data("totalCount", count).data("pageSize", length).data("pageIndex", start).data("repairs", repairs);
        return r;
    }

    @Override
    public HashMap searchRepairsInfo(Integer id) {
        HashMap map = repairsDao.searchRepairsInfo(id);
        String imagesStr = (String) map.get("images");
        List<String> images = arrayConversion(imagesStr);
        map.put("images", images);
        return map;
    }

    @Transactional
    @Override
    public R insert(TbRepairs repairs) {
        // 1、获取设备的详细信息
        Integer facilityId = repairs.getFacilityId();
        HashMap map = facilityService.searchFacilityInfo(facilityId);
        // 1.1、获取总数量
        Integer amount = (Integer) map.get("amount");
        // 1.2、查看报修的数量是不是比总数量还要多
        if (repairs.getAmount() > amount){
            return R.error().message("报修数量大于设备总数量");
        }
        if (repairs.getAmount() < 1){
            return R.error().message("报修数量最少为1");
        }
        // 2、设置设备的数量
        amount = amount - repairs.getAmount();
        TbFacility facility = JSONUtil.parse(map).toBean(TbFacility.class);
        facility.setAmount(amount);
        // 3、报修保存
        int userId = StpUtil.getLoginIdAsInt();
        repairs.setUserId(userId);
        repairs.setApplyTime(new Date());
        repairs.setStatus(0);
        // 更新设备的状态
        facilityService.updateFacilityStatus(facilityId, 1);
        // 添加报修信息
        int rows = repairsDao.insert(repairs);
        if(rows == 0){
           return R.error();
        }
        // 4、更新设备数量
        rows = facilityService.update(facility);
        return rows > 0 ? R.ok() : R.error();
    }

    @Override
    public R repairsAccomplish(Integer id) {
        // 1、获取报修的信息
        HashMap param = this.searchRepairsInfo(id);
        // 1.1、报修的人
//        Integer userId = (Integer) param.get("userId");
        // 1.2、报修商品
        Integer facilityId = (Integer) param.get("facilityId");
        // 1.3、报修的数量
        Integer amount = (Integer) param.get("amount");
        // 2、获取设备的信息
        HashMap facilityMap = facilityService.searchFacilityInfo(facilityId);
        // 2.1、获取设备的总数量
        Integer totalAmount = (Integer) facilityMap.get("amount");
        // 2.2、设置新的数量
        totalAmount += amount;
        facilityMap.put("amount",totalAmount);
        // 3、更改维修的状态
        Map<String, Integer> map = new HashMap<>();
        map.put("id", id);
        map.put("status", 1);
        int rows = repairsDao.updateFacilityStatus(map);
        if (rows == 0){
            return R.error();
        }
        // 4、更新设备信息
        TbFacility facility = JSONUtil.parse(facilityMap).toBean(TbFacility.class);
        rows = facilityService.update(facility);
        facilityService.updateFacilityStatus(facilityId, 0);
        return rows > 0 ? R.ok() : R.error();
    }

    @Override
    public R breakage(Integer id) {
        HashMap repairsInfo = repairsDao.searchRepairsInfo(id);
        Map<String, Integer> map = new HashMap<>();
        map.put("id", id);
        map.put("status", 2);
        Integer facilityId = (Integer) repairsInfo.get("facilityId");
        facilityService.updateFacilityStatus(facilityId, 3);
        int rows = repairsDao.updateFacilityStatus(map);
        return rows > 0 ? R.ok() : R.error();
    }

}
